<?php
/**
 * 制作:  叶风
 * Qq:    355007778
 * Email: ye.fn@163.com
 *
 */
namespace c\img;
class b extends \control
{
    var $class = array('F'=>'files');
 
    /**
     *  $f 文件地址
     *  $w 宽, 只设宽, 则高为比例值,
     *  $h 高, 只设高, 则宽为比例值
     *  $q 加密的参数
     */
	function index_home()
	{
        $g = $_GET;
		if ($g['q'])
         {
		 	$q = \encrypt::q_de($g['q']);
			parse_str($q, $arr);
			$g = array_merge($g, $arr);
		 }
        
       
	    $w = intval($g['w']);
		$h = intval($g['h']);
        $f = $g['f'];
        
        // 原图
        if (preg_match("/^http:|^https:/", $f))
         {
            $a = get_headers($f, 1);
			if ($w<1 && $h<1)
                \url::jump($f);
            $cft = strtotime($a['Last-Modified']);
         }
        else{
            $f = FILE_DIR . $this->F->charset_sys($f);
            if ($w<1 && $h<1)
                $this->v($f);
            $cft = @filemtime($f);
         }
		
         
        // 已剪截的缓存文件
        $cfile = CACHE_DIR . 'img/'.  md5($f) ."-{$w}_{$h}.". $this->extName($f);
        if (is_file($cfile) && (filemtime($cfile)==$cft) )
            $this->v($cfile);
        
        
        // 剪截
        $ima = $this->im($f);
        $o_w = imagesx($ima);
        $o_h = imagesy($ima);
        $o_x = $o_y = $n_h = $n_w = 0;

        if( !$g['all'] )
         {
            if($w && $w*10 < $o_w)
             { 	
                $temp = $w*10;
                $o_x = ($o_w-$temp )/2;
                $o_w = $temp;	
             }
            if($h && $h*10 < $o_h )
             {
                $temp = $h*10;
                $o_y = ($o_h- $temp)/2;
                $o_h = $temp;
             }	
         }

        //按宽缩放
        if($w)
         {  		 		
            if($g['bw'] && $o_w<$w)
                $w = $o_w;
            $n_w = $w;
            $n_h = $w / $o_w * $o_h;
         }
        //按高缩放
        if($h)
         {  
            if($h<$n_h)
             {
                $n_h = $h;
                $o_y = $o_h;
                $o_h = $o_w /$w * $h;
                $o_y = ($o_y - $o_h)/2;
             }
            elseif($h>$n_h)
             {
                $n_h = $h;
                $n_w = $h / $o_h * $o_w;
                if($w)
                 {
                    if($n_w>$w)
                     {
                        $n_w = $w;
                        $o_x = $o_w;
                        $o_w = $o_h /$h * $w;
                        $o_x = ($o_x - $o_w) /2;
                     }
                 } 
             }
         }

        $new = imagecreatetruecolor($n_w, $n_h);
        imagecopyresized($new, $ima, 0, 0, $o_x, $o_y, $n_w, $n_h, $o_w, $o_h);

        $this->F->c_dir(dirname($cfile));
        $this->imOut($cfile, $new);
        touch($cfile, $cft);
        $this->v($cfile);      
	}
    /**
     * 
     * @param type $f
     */
    function v($f)
    {
        if (!is_file($f) )
         {
            header("HTTP/1.1 404 Not Found");
            die;
         }
        $fileStat = stat($f);
        $etag = $fileStat['mtime'] .  $fileStat['size']; 
	    $etag = md5($etag) . '-' . crc32($etag); 
	    header("ETag: $etag");
        header("Last-Modified:". gmdate('r', $fileStat['mtime']));
	    header('Cache-Control: public, must-revalidate, max-age=0');
        header('Pragma: cache');
	    if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH']==$etag) 
	      { 
	        header("HTTP/1.1 304 Not Modified"); 
	        die; 
	      }
        
        header("Content-type: image/jpeg");
        header("Content-Disposition: filename=" . basename($f));
        if ($fileStat['size']<500)
         {
            echo file_get_contents($f);
            die;
         }
        $fp = fopen($f, "rb"); 
        do{
            echo fread($fp, 500); 
	       	ob_flush();
        }while (!feof($fp)); 
	    fclose($fp);
        die;
    }
    
    /**
     * 水印
     */
    function watermark($f)
    {
		if (!\YF_f::$config['file_watermark']=='')
			return true;
        
		$dst = $this->im($f);
		$d_x = imagesx($dst);
		$d_y = imagesy($dst);

		if (\YF_f::$config['file_watermark_type']=='pic')
		 {	
            $src = $this->im(\YF_f::$config['file_watermark']);
			$x = imagesx($src);
			$y = imagesy($src);
			imagecopymerge($dst, $src, $d_x-$x, $d_y-$y, 0, 0, $x, $y, 50);
		 }
		else{
			$font = PROCESSOR_DIR . 'language/'. \YF_f::$config['file_watermark_font'] . '.ttf';
			$fontcolor = imagecolorallocate($dst, 255, 255, 255);
			$fontsize = 20;
			$x = $d_x - strlen(\YF_f::$config['file_watermark'])* $fontsize;
			$y = $d_y - 10;
			imagettftext($dst, $fontsize, 0, $x, $y, $fontcolor, $font, \YF_f::$config['file_watermark']);
		  }
		return $this->imOut($f, $dst);
    }
    
    /**
     * 生成图片文件
     * 
     * @param type $out_file
     * @param type $src
     * @return type
     */    
    function imOut($file, $im)
    {
        $ext = $this->extName($file);
        switch ($ext){
            case 'jpg':
                return imagejpeg($im, $file, 100);
            case 'gif':
                return imagegif($im, $file);
            case 'png':
                return imagepng($im, $file);
         }
        return imagejpeg($im, $file);
    }
    
    /**
     * 读取图像资源
     */
    function im($f)
    {
        $str = $this->F->r_file($f);
        if (!$str)
            \YF_f::error(\lang::g('Over'));
        $str = imagecreatefromstring($str);
        if ($str==false)
            \YF_f::error(\lang::g('Over'));
        return $str;
    }
    
    function extName($file)
    {
        $a = $this->F->get_ext($file);
        if (!in_array($a, array('jpg','jpeg','gif','png')))
            $a = 'png';
        return $a;
    }
}
?>
